﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Bitirme.Controllers;

namespace Bitirme.Views
{
    public partial class AddEmployee : System.Web.UI.Page
    {
         Dictionary<string, int> capabilityMap = new Dictionary<string, int>();
         SkillHandler skillHandler;

        protected void Page_Load(object sender, EventArgs e)
        {
            //capabilityMap = new Dictionary<string, int>();
            skillHandler = new SkillHandler();
        }

        protected void ParseCapabilities(string capabilities, string skills)
        {
            capabilities = capabilities.Remove(capabilities.Length - 1, 1);
            skills = skills.Remove(skills.Length - 1, 1);
            string[] capabilityArray = capabilities.Split(new string[] { "," }, StringSplitOptions.None);
            string[] skillArray = skills.Split(new string[] { "," }, StringSplitOptions.None);
            int i = 0;
            foreach (string capability in capabilityArray)
            {
                capabilityMap.Add(capability, Convert.ToInt32(skillArray[i]));
                i++;
            }
            
        }

        /*** Çalışan Ekle ***/
        protected void Button1_Click(object sender, EventArgs e)
        {
            EmployeeHandler employeeHandler = new EmployeeHandler();
            string name = NameTextBox.Text;
            string surname = SurnameTextBox.Text;
            string email = EmailTextBox.Text;
            int salaryPerDay = Convert.ToInt32(SalaryPerDayTextBox.Text);
            int hourPerDay = Convert.ToInt32(HourPerDayTextBox.Text);

            /*** Capabilities ve skill levelleri parse edilip map'e yazılıyor ***/
            ParseCapabilities(CapabilityString.Value, SkillString.Value);

            /*** Capability tablosuna yeni capabilityler ekleniyor ***/
            Dictionary<int, int> capabilityResult = new Dictionary<int, int>();
            try
            {
                capabilityResult = skillHandler.InsertCapability(capabilityMap);
                if (capabilityResult.Count > 0)
                    InsertedCapability.Text = "Yetenek Kaydı Yapıldı.";
                else
                    InsertedCapability.Text = "Hata: Yetenek Kaydı Yapılamadı!";
            }

            catch (Exception ee)
            {
                InsertedCapability.Text = ee.Message.ToString();
            }

            finally
            {
                skillHandler = null;
            }


            /*** Employee ekleme ***/
            int intResult = 0;
           
            try
            {
                intResult = employeeHandler.InsertEmployee(name, surname, email, salaryPerDay, hourPerDay);
             

                if (intResult > 0)
                    ResultMessage.Text = "Çalışan Kaydı Oluşturuldu. :" + intResult;
                else
                    ResultMessage.Text = "Hata: Çalışan Kaydı Yapılamadı!";
            }

            catch (Exception ee)
            {
                ResultMessage.Text = ee.Message.ToString();
            }

            /*** Capability to Employee ***/
            int result = 0;

            try
            {
                result = employeeHandler.InsertEmployeeToCapability(intResult, capabilityResult);

                if (result > 0)
                    ResultMessage.Text = "Çalışan Kaydı Oluşturuldu. :" + result;
                else
                    ResultMessage.Text = "Hata: Çalışan Kaydı Yapılamadı!";
            }

            catch (Exception ee)
            {
                ResultMessage.Text = ee.Message.ToString();
            }

            finally
            {
                employeeHandler = null;
            }
        }

        /*** Yetenek Ekle ***/
        
        protected void EnterButon_Click(object sender, EventArgs e)
        {
            /*
            string capability = CapabilityTextBox.Text;
            int level=0;
            if (Radio1.Checked) level = 3;
            if (Radio2.Checked) level = 2;
            if (Radio3.Checked) level = 1;
            InsertedCapability.Text = capability;
            capabilityMap.Add(capability, level);
           

            int intResult;
            try
            {
                intResult = skillHandler.InsertCapability(capabilityMap);
                if (intResult > 0)
                    InsertedCapability.Text = capability+ " id: " + intResult;
                else
                    InsertedCapability.Text = "Hata: Yetenek Kaydı Yapılamadı!";
            }

            catch (Exception ee)
            {
                InsertedCapability.Text = ee.Message.ToString();
            }

            finally
            {
                skillHandler = null;
            }
         * * */
        }
         
    }
}
